/*
 * @Author: Mcfly
 * @Date: 2021-11-04 14:35:17
 * @LastEditTime: 2021-11-04 15:49:27
 * @LastEditors: Mcfly
 * @Description: 
 * @FilePath: \FTTfly\BSPdriver\Laser.c
 * 本人github:https://github.com/McflyWZX
 */
#include "Laser.h"
#include "stdlib.h"
#include "string.h"
#include "hw_gpio.h"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_sysctl.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"

/**
 * @description: 
 * @param {uint32_t} portPerip
 * @param {uint32_t} port
 * @param {uint8_t} pin
 * @param {float} deltaTime 调用处理函数的周期
 * @param {float} cycleTime 激光变化周期
 * @param {float} flashTime 激光一个周期内亮的时间
 * @return {Laser *} laser
 * @Author: Mcfly
 * @Date: 2021-11-04 14:54:00
 */
Laser *Laser_Init(uint32_t portPerip, uint32_t port, uint8_t pin, float deltaTime, float cycleTime, float flashTime)
{
    SysCtlPeripheralEnable(portPerip);
    GPIOPinTypeGPIOOutput(port, pin);
    GPIOPadConfigSet(port, pin, GPIO_STRENGTH_12MA, GPIO_PIN_TYPE_STD);
    //GPIOPinWrite(port, pin, 0);
    Laser *laser = (Laser *)malloc(sizeof(Laser));
    laser->port = port;
    laser->pin = pin;
    laser->flashTime = flashTime;
    laser->cycleTime = cycleTime;
    laser->deltaTime = deltaTime;
    laser->workTime = 0;
    laser->enable = 0;
    return laser;
}

void Laser_Process(Laser *laser)
{
	if(laser->enable)
	{
		laser->workTime += laser->deltaTime;
		if(laser->workTime >= laser->cycleTime)
		{
			laser->workTime = 0;
		}
		GPIOPinWrite(laser->port, laser->pin, laser->pin * (laser->workTime > laser->flashTime));
	} else {
		laser->workTime = 0;
		GPIOPinWrite(laser->port, laser->pin, laser->pin);
	}
}
